#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QRegExpValidator>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit_name->setPlaceholderText("请输入用户名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_passward->setPlaceholderText("请输入密码");
    ui->lineEdit_passward->setClearButtonEnabled(true);
    ui->lineEdit_passward->setEchoMode(QLineEdit::Password);

    ui->lineEdit_phone->setPlaceholderText("请输入手机号");
    ui->lineEdit_phone->setClearButtonEnabled(true);
//    ui->lineEdit_phone->setInputMask("000-0000-0000");

    // 使用正则表达式对手机号进行限制
    QRegExp regExp("^1\\d{10}$");
    // 将验证器关联到LineEdit上
    ui->lineEdit_phone->setValidator(new QRegExpValidator(regExp));
    ui->lineEdit_regexp->setValidator(new QRegExpValidator(regExp));
    ui->pushButton_regexp->setEnabled(false);
    ui->pushButton->setEnabled(false);

    // 判断密码是否相同
    ui->lineEdit_1->setEchoMode(QLineEdit::Password);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);

    // 手动显示密码
    ui->lineEdit_showpassward->setEchoMode(QLineEdit::Password);
    ui->lineEdit_showpassward->setClearButtonEnabled(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    // isChecked：是否被选中
    QString echo = ui->radioButton_male->isChecked() ? "男":"女";

    qDebug() << "姓名：" << ui->lineEdit_name->text() <<
                "\n密码：" << ui->lineEdit_passward->text() <<
                "\n性别：" << echo <<
                "\n手机号：" << ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_regexp_textEdited(const QString &arg1)
{
    QString content = arg1;
    int pos = 0;
    if(ui->lineEdit_regexp->validator()->validate(content,pos) == QValidator::Acceptable)
        ui->pushButton_regexp->setEnabled(true);
    else
        ui->pushButton_regexp->setEnabled(false);
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    QString content = arg1;
    int pos = 0;
    if(ui->lineEdit_phone->validator()->validate(content,pos) == QValidator::Acceptable)
        ui->pushButton->setEnabled(true);
    else
        ui->pushButton->setEnabled(false);
}

void Widget::on_lineEdit_1_textEdited(const QString &arg1)
{
    (void)arg1;
    this->issame();
}

void Widget::on_lineEdit_2_textEdited(const QString &arg1)
{
    (void)arg1;
    this->issame();
}

void Widget::issame()
{
    QString pw1 = ui->lineEdit_1->text();
    QString pw2 = ui->lineEdit_2->text();
    if(pw1.isEmpty() && pw2.isEmpty())
        ui->label_isempty->setText("密码为空");
    else if(pw1 == pw2)
        ui->label_isempty->setText("密码相同");
    else
        ui->label_isempty->setText("密码不同");
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
        ui->lineEdit_showpassward->setEchoMode(QLineEdit::Normal);
    else
        ui->lineEdit_showpassward->setEchoMode(QLineEdit::Password);
}
